//
//  PhotoBrowserCell.swift
//  zyqWeibo
//
//  Created by 张亚琼 on 15/7/5.
//  Copyright © 2015年 张亚琼. All rights reserved.
//

import UIKit
import SDWebImage
import FFAutoLayout

class PhotoBrowserCell: UICollectionViewCell,UIScrollViewDelegate {
    
    var imageUrl:NSURL? {
        didSet {
            // 重置
            resetScrollView()
            // 开始菊花
            indicator.startAnimating()
//            imageView.sd_setImageWithURL(imageUrl)
            imageView.sd_setImageWithURL(imageUrl, placeholderImage: nil) { (image, error, _, _) -> Void in
                if image == nil {
                    return
                }
                self.setPosition(image)
                self.indicator.stopAnimating()
            }
        }
    }
    
    func resetScrollView() {
        scrollView.contentOffset = CGPointZero
        scrollView.contentSize = CGSizeZero
        scrollView.contentInset = UIEdgeInsetsZero
    }
    
    // 设置位置
    func setPosition(image:UIImage) {
        // 回调的时候设置大小(闭包内使用self.)
        // 为了让图片居中，所以计算y值
        let size = self.calSizeWithImage(image)// 图片尺寸
        
        // 判断是否是长图
        if bounds.height < size.height {
            // 是长图
            imageView.frame = CGRect(origin: CGPointZero, size: size)
            // 为了可以滚动，要设置scrollview
            scrollView.contentSize = size
        }else {
            
            let y = (bounds.size.height - size.height) * 0.5
            imageView.frame = CGRect(origin: CGPointZero, size: calSizeWithImage(image))
            scrollView.contentInset = UIEdgeInsets(top: y, left: 0, bottom: 0, right: 0)
        }
        
        
    }
    private func calSizeWithImage(image:UIImage) -> CGSize{
        // 比例
        let scale = image.size.height / image.size.width
        let h = bounds.width * scale
        return CGSize(width: bounds.width, height: h)
    }
  
    // 构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 添加控件
        scrollView.frame = bounds
        addSubview(scrollView)
        scrollView.addSubview(imageView)
        
        addSubview(indicator)
        
        // 设置代理和缩放比例
        scrollView.delegate = self
        scrollView.minimumZoomScale = 0.5
        scrollView.maximumZoomScale = 2
        
        indicator.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil)
    }
    
    //MARK: scrollView的代理方法
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageView
    }
    
    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
        var offsetX = (scrollView.bounds.width - view!.frame.width) * 0.5
        if offsetX < 0 {
            offsetX = 0
        }
        var offsetY = (scrollView.bounds.height - view!.frame.height) * 0.5
        if offsetY < 0 {
            offsetY = 0
        }
        // 重新设置边距，保证是左上角不超出
        scrollView.contentInset = UIEdgeInsets(top: offsetY, left: offsetX, bottom: 0, right: 0)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: 懒加载
    lazy var imageView:UIImageView = UIImageView()
    lazy private var scrollView:UIScrollView = UIScrollView()
    // 指示器
    lazy private var indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
    
}
